package com.fenghongzhang.day015;

import android.app.AlertDialog;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    //子线程->下载
    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            URL url=new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//            urlConnection.setReadTimeout(3*1000);
//            urlConnection.connect();
            if(urlConnection.getResponseCode() == 200){
                InputStream inputStream = urlConnection.getInputStream();

                StringBuilder stringBuilder = new StringBuilder();
                byte[] b=new byte[1024];
                int len=0;
                while((len=inputStream.read(b))!=-1){
                    stringBuilder.append(new String(b,0,len));
                }

                //发送action
                String s = stringBuilder.toString();
                //用广播发送消息
                Intent intent1 = new Intent();
                //频道
                intent1.setAction("com.feng.json");
                Bundle bundle = new Bundle();
                bundle.putString("json",s);
                intent1.putExtras(bundle);
                sendBroadcast(intent1);
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
